package com.gxa.ssm340.service.impl;

import com.gxa.ssm340.common.ErrorCode;
import com.gxa.ssm340.entity.dto.*;
import com.gxa.ssm340.entity.pojo.Manager;
import com.gxa.ssm340.entity.pojo.Points;
import com.gxa.ssm340.entity.vo.AdminMenusVO;
import com.gxa.ssm340.entity.vo.ManagerPageVO;
import com.gxa.ssm340.entity.vo.PointGetVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.mapper.AdminMapper;
import com.gxa.ssm340.service.AdminService;
import com.gxa.ssm340.uitl.JwtUtil;
import com.gxa.ssm340.uitl.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 管理员业务接口实现类
 * @author 赵文川
 * @Description: $
 * @date 2023/10/20 19:04
 */
@Transactional(rollbackFor = Throwable.class)
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 管理员新增员工业务
     * @param dto 存储员工手机号，密码，姓名
     * @return
     * @throws BizException
     */
    @Override
    public Boolean addManager(ManagerRegDTO dto) throws BizException {
        // 校验注册数据是否为空
        if(dto.getName() == null || dto.getPhone() == null || dto.getPwd() == null) {
            throw new BizException(ErrorCode.NULL_FIELD);
        }
        // 校验该员工手机号是否已经注册
        Manager manager = adminMapper.selectByPhone(dto.getPhone());
        if(manager != null) {
            throw new BizException(ErrorCode.WRONG_REG_PHONE);
        }
        // 使用Jwt对密码加密
        String newPwd = MD5Utils.encrypt(dto.getPwd(), MD5Utils.MD5_KEY);
        // 设置新密码
        dto.setPwd(newPwd);
        // 根据员工信息注册
        adminMapper.insertMgr(dto);
        return true;
    }

    /**
     * （根据员工姓名/手机号）查询员工信息
     * @param dto
     * @return
     * @throws BizException
     */
    @Override
    public ManagerPageVO getManagers(ManagerPageDTO dto) throws BizException {
        List<Manager> managers = adminMapper.selectManagers(dto);
        List<Manager> allManager = adminMapper.selectAllManager(dto);
        if(allManager == null) {
            throw new BizException(ErrorCode.NULL_USER);
        }
        ManagerPageVO managerPageVO = new ManagerPageVO();
        // 设置所有员工信息
        managerPageVO.setManagers(managers);
        // 设置总记录数
        managerPageVO.setCount(allManager.size());
        return managerPageVO;
    }

    @Override
    public Boolean removeManager(String phone) throws BizException {
        // 判断特征值是否为空
        if(phone == null && phone == "") {
            throw new BizException(ErrorCode.NULL_FIELD);
        }
        // 执行删除操作
        Integer result = adminMapper.deleteManagerById(phone);
        // 判断删除结果是否成功
        if(result == null) {
            throw new BizException(ErrorCode.NULL_USER);
        }
        return true;
    }

    @Override
    public Boolean modifyManagerIsUse(ManagerIsUseDTO dto) throws BizException {
        if(dto.getPhone() == null || dto.getIsUse() == null) {
            throw new BizException(ErrorCode.NULL_FIELD);
        }
        Integer result = adminMapper.updateIsUseByPhone(dto);
        if(result == null) {
            throw new BizException(ErrorCode.NULL_USER);
        }
        return true;
    }



      @Override
    public AdminMenusVO AdminLogin(AdminLoginDTO adminLoginDTO) throws BizException {

        AdminMenusVO adminMenusVO = adminMapper.selectByPhoneAndPwd(adminLoginDTO);
        String token = JwtUtil.sign(adminLoginDTO.getPhone(), 30 * 60 * 1000L);
        adminMenusVO.setToken(token);
        if(adminMenusVO==null){
            throw  new BizException(ErrorCode.FAILD_LOGIN);
        }
        return adminMenusVO;
    }

    @Override
    public AdminMenusVO getMenusByphone(String phone) throws BizException {

        return adminMapper.selectMenusByPhone(phone);
    }

    @Override
    public PointGetVO getIncomePoints(PointGetDTO dto) throws BizException {
        if(dto.getPage()==null){
            throw new BizException(ErrorCode.NULL_FIELD);
        }
        // 获取用户获取的积分信息
        List<Points> points = adminMapper.selectPointGetByPhone(dto);
        PointGetDTO pointGetDTO = new PointGetDTO();
        pointGetDTO.setPhone(dto.getPhone());
        // 获取总记录数
        int count = adminMapper.selectPointGetByPhone(pointGetDTO).size();
        if(points==null){
            throw new BizException(ErrorCode.NULL_USER);
        }
        PointGetVO pointGetVO = new PointGetVO();
        pointGetVO.setPoints(points);
        pointGetVO.setCount(count);
        return pointGetVO;
    }

}
